package com.homegoods.designer.presenter;

import android.annotation.SuppressLint;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.homegoods.aroutercomment.ARouterPath;
import com.homegoods.classify.model.Constant;
import com.homegoods.designer.DesignerApiUtils;
import com.homegoods.designer.model.DesignerCaseModel;
import com.homegoods.designer.model.DesignerHomeModel;
import com.homegoods.designer.ui.view.IDesignerHomeView;
import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.mvp.BasePresenter;
import com.kedll.library.util.Tuple2;

import java.util.List;
import java.util.Random;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author QiQi
 */
public class DesignerHomePresenter extends BasePresenter<IDesignerHomeView> {

    DesignerHomeModel homeModel;
    private MultiTypeAdapter adapter = new MultiTypeAdapter();
    private String id;


    public DesignerHomePresenter(IDesignerHomeView view, String id) {
        super(view);
        this.id = id;
    }

    public MultiTypeAdapter getAdapter() {
        return adapter;
    }

    @SuppressLint("CheckResult")
    public void loadData() {
        mView.showLoad("获取数据中...");
        DesignerApiUtils.getDesignerDetails(id, designerHomeModel -> {
            mView.setHomeData(designerHomeModel);
            for (DesignerCaseModel model : designerHomeModel.getWorkslist()) {
                adapter.addItem(model.createItem(0, adapter));
            }
            adapter.notifyItemRangeInserted(0, designerHomeModel.getWorkslist().size());
            mView.hideLoad();
//            Observable<DesignerHomeModel> homeModelObservable = Observable.just(homeModel);
//            Observable<List<DesignerCaseModel>> caseObservable = Observable.just(homeModel.getWorkslist());
//
//            Observable.zip(homeModelObservable, caseObservable, Tuple2::new)
//                    .subscribeOn(Schedulers.io())
//                    .observeOn(AndroidSchedulers.mainThread())
//                    .subscribe(r -> {
//
//                    }, throwable -> System.out.println(throwable.getMessage()));
        }, throwable -> {

        });
//        homeModel = new DesignerHomeModel();
//        homeModel.setCity("浙江 温州");
//        homeModel.setFans(20);
//        homeModel.setUndergo("5年设计经验");
//        homeModel.setAttention(400);
//        homeModel.setBgimg(Constant.TEST_IMG_URL + new Random().nextInt(10));
//        homeModel.setName("王博");
//        homeModel.setMakeoffers("300-400");
//        homeModel.setRange("软装+硬装");
//        homeModel.setId("0");
//        homeModel.setIntro("纵观室内设计所从事的工作，其包括了艺术和技术两个方面。室内设计就是为特定的室内环境提供整体的、富有创造性的解决方案，它包括概念设计、运用美学和技术上的办法以达到预期的效果。“特定的室内环境”是指一个特殊的、有特定目的");

//        Observable<DesignerHomeModel> homeModelObservable = Observable.just(homeModel);
//        Observable<List<DesignerCaseModel>> caseObservable = Observable
//                .range(0, 4)
//                .map(integer -> {
//                    DesignerCaseModel caseModel = new DesignerCaseModel();
//                    caseModel.setHuxing("四室一厅");
//                    caseModel.setBgimg(Constant.TEST_IMG_URL + new Random().nextInt(20));
//                    caseModel.setMianji("400平");
//                    caseModel.setTitle("古典就是这么美");
//                    caseModel.setId(integer.toString());
//                    return caseModel;
//                })
//                .buffer(4);
//        Observable.zip(homeModelObservable, caseObservable, Tuple2::new)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(r -> {
//                    DesignerHomeModel home = r.getT0();
//                    List<DesignerCaseModel> caseModels = r.getT1();
//                    mView.setHomeData(home);
//                    for (DesignerCaseModel model : caseModels) {
//                        adapter.addItem(model.createItem(0, adapter));
//                    }
//                    adapter.notifyItemRangeInserted(0, caseModels.size());
//                    mView.hideLoad();
//                }, throwable -> System.out.println(throwable.getMessage()));

    }

    public boolean isMine() {
        return mView.isMine();
    }

    /**
     * 显示更多案例
     *
     * @param view
     */
    public void onMoreClick(View view) {
        ARouter.getInstance()
                .build(ARouterPath.PATH_MORE_CASE)
                .withObject("data", homeModel)
                .navigation();
    }

    /**
     * 编辑个人资料
     *
     * @param view
     */
    public void onEditClick(View view) {
        ARouter.getInstance()
                .build(ARouterPath.PATH_DESIGNER_EDIT)
                .navigation();
    }

    /**
     * 关注
     *
     * @param view
     */
    public void onAttentionClick(View view) {

    }

    /**
     * 聊天
     *
     * @param view
     */
    public void onChatClick(View view) {

    }
}